package TreeDayShuZu;

import org.junit.Test;

public class Dome10 {
    //冒泡排序基础版本
    @Test
    public void test01(){
        int[] arr = {450, 23, 78, 100, 56, 90};
        int temp=0;
        for (int i = 0; i < 5; i++) {
            if (arr[i]>arr[i+1]){
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        System.out.println("第一次排序的结果为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        for (int i = 0; i < 4; i++) {
            if (arr[i]>arr[i+1]){
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        System.out.println("\n第二次排序的结果为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        for (int i = 0; i < 3; i++) {
            if (arr[i]>arr[i+1]){
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        System.out.println("\n第三次排序的结果为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        for (int i = 0; i < 2; i++) {
            if (arr[i]>arr[i+1]){
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        System.out.println("\n第四次排序的结果为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        for (int i = 0; i < 1; i++) {
            if (arr[i]>arr[i+1]){
                temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        System.out.println("\n第五次排序的结果为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
    //冒泡排序的进阶版本
    @Test
    public void test02(){
        int[] arr = {24, 68, 80, 57, 13};
        int temp=0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
            System.out.println("\n第"+(i+1)+"次排序的结果为");
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[j]+" ");
            }
        }
    }
    @Test
    //冒泡排序最终版本
    public void test03(){
        int[] arr = {24, 68, 80, 57, 13};
        int temp=0;
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
            System.out.println("\n第"+(i+1)+"次排序的结果为");
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[j]+" ");
            }
        }
    }
}
